package com.xuecheng.content.api;

import com.alibaba.fastjson.JSON;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.dto.TeachPlanDto;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.service.CoursePublishService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 课程预览，发布
 */
@Controller // 注意这里不能使用RestController 因为RestController 返回的是json数据
// 我们现在要返回的是 前端页面 所以使用Controller注解而不是RestController
@RequiredArgsConstructor
public class CoursePublishController {

    private final CoursePublishService coursePublishService;

    /**
     * 课程预览
     *
     * @param courseId 课程id
     * @return 课从视预览页面
     */
    @GetMapping("/coursepreview/{courseId}")
    public ModelAndView preview(@PathVariable("courseId") Long courseId) {
        ModelAndView modelAndView = new ModelAndView();
        CoursePreviewDto coursePreviewInfo = coursePublishService.getCoursePreviewInfo(courseId);
        modelAndView.addObject("model", coursePreviewInfo);
        modelAndView.setViewName("course_template");
        return modelAndView;
    }

    /**
     * 提交审核
     *
     * @param courseId 课程id
     */
    @ResponseBody
    @PostMapping("/courseaudit/commit/{courseId}")
    public void commitAudit(@PathVariable("courseId") Long courseId) {
        Long companyId = 1232141425L;
        coursePublishService.commitAudit(companyId, courseId);
    }

    /**
     * 课程发布
     *
     * @param courseId 课程id
     */
    @ApiOperation("课程发布")
    @ResponseBody
    @PostMapping("/coursepublish/{courseId}")
    public void coursePublish(@PathVariable("courseId") Long courseId) {
        Long companyId = 1232141425L;
        coursePublishService.coursePublish(companyId, courseId);
    }

    /**
     * 根据课程id查询课程发布信息
     *
     * @param courseId 课程id
     * @return 课程发布信息
     */
    @ApiOperation("根据课程id查询课程发布信息")
    @ResponseBody
    @GetMapping("/r/coursepublish/{courseId}") // 路径有/r/ 前缀，表示是内容的接口可以直接访问，不用传递令牌
    public CoursePublish getCoursepublish(@PathVariable("courseId") Long courseId) {
        return coursePublishService.getCoursePublish(courseId);
    }

    /**
     * 获取课程发布信息
     *
     * @param courseId 课程id
     * @return 课程预发布信息
     */
    @ApiOperation("获取课程发布信息")
    @ResponseBody
    @GetMapping("/course/whole/{courseId}")
    public CoursePreviewDto getCoursePublish(@PathVariable("courseId") Long courseId) {
        CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
        // 查询课程发布表
        // CoursePublish coursePublish = coursePublishService.getCoursePublish(courseId);
        // 先从缓存中查 没有在查数据库
         CoursePublish coursePublish = coursePublishService.getCoursePublishCache(courseId);
        if (coursePublish == null) {
            return coursePreviewDto;
        }

        // 封装数据
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(coursePublish, courseBaseInfoDto);
        coursePreviewDto.setCourseBase(courseBaseInfoDto);

        // 取出课程计划信息
        String teachPlanJson = coursePublish.getTeachplan();
        // 把json字符产装成List集合
        List<TeachPlanDto> teachPlanDtos = JSON.parseArray(teachPlanJson, TeachPlanDto.class);
        coursePreviewDto.setTeachplans(teachPlanDtos);

        return coursePreviewDto;
    }
}